 //决策
#pragma once
#include"Decision.h"
using namespace std;
using namespace Dec;
namespace RobotDec
{
       // #define  DES_SPACE  "-"
    // #define  DES_SPACE_LEN  strlen(DES_SPACE)
    //记忆信息
    struct Rember
    {
        size_t _action;//动作
        size_t _emotion;//表情
        size_t _attitude;//态度
        size_t _model;//模式
        size_t _name;//身份
        double _probability;//执行几率
        Rember()
            :_action(0),
            _emotion(0),
            _attitude(0),
            _model(0),
            _name(0),
            _probability(0.0)
        {}
        void AddProbility(double x)
        {
            _probability += x;
            if (_probability > 1.0)
            {
                _probability = 1.0;
            }
        }
        void DelProbility(double x)
        {
            _probability -= x;
            if (_probability < 0.0)
            {
                _probability = 0.0;
            }
        }
        bool operator==(const Rember& other)const
        {
            if (_action == other._action && _emotion == other._emotion)
            {
                return true;
            }
            return false;
        }
    };

    template<class T>
    class RemberCache
    {
    private:
        queue<T> _ls;//循环队列
        T _frember;//上一次记忆
        T _srember;//上上次记忆
        int _size = 5;
    public:
        ~RemberCache() {};
        RemberCache()
            :_ls(queue<T>()),
            _frember(T()),
            _srember(T())
        {}
        void Resize(size_t size)
        {
            _size = size;
        }
        RemberCache(const RemberCache<T>& rc)
        {
            queue<T> tmp = rc._ls;
            while (!tmp.empty())
            {
                _ls.push(tmp.front());
                tmp.pop();
            }
            _size = rc._size;
            _frember = rc._frember;
            _srember = rc._srember;
        }
        RemberCache<T>& operator=(const RemberCache<T>& rc)
        {
            if (this != &rc)
            {
                while (!_ls.empty())
                {
                    _ls.pop();
                }
                queue<T> tmp = rc._ls;
                while (!tmp.empty())
                {
                    _ls.push(tmp.front());
                    tmp.pop();
                }
                _size = rc._size;
                _frember = rc._frember;
                _srember = rc._srember;
            }
            return *this;
        }
        bool is_last_rember()
        {
            return _ls.size() > 1;
        }
        T& get_first()
        {
            return _frember;
        }
        T& get_second()
        {
            return _srember;
        }
        bool empty()
        {
            return _ls.empty();
        }
        //历史偏向 上一次，上上次，
        pair<bool, T> history_inc()
        {
            if (_ls.empty())
                return { false,T() };
            if (get_first() == get_second() && get_first() == _ls.front())
            {
                return { true,_ls.front() };
            }
            else
            {
                return { false,_ls.front() };
            }
        }
        void Push(const T rem)
        {
            if (_ls.size() >= _size)
            {
                _ls.pop();
            }
            _srember = _frember;
            if (!_ls.empty())
                _frember = _ls.back();
            _ls.push(rem);
        }
    };

    //身份信息
    struct Identity
    {
        size_t _name;//名字
        RemberCache<Rember> _mrem;//近期本人记忆存储
        Rember _history;//近期记忆的偏向 
        double _mode;//历史模式
        std::bitset<64> _actions;//激活的命令
        unordered_map<size_t, size_t> _order_count;//命令执行次数
        size_t _faction;//常用动作
        Identity(size_t name = 0, RemberCache<Rember> mrem = RemberCache<Rember>(), Rember history = Rember()
            , double mode = 50.0, std::bitset<64> action = std::bitset<64>(), unordered_map<size_t, size_t> order_count = unordered_map<size_t, size_t>())
            :_name(name),
            _mrem(mrem),
            _history(history),
            _mode(mode),
            _actions(action),
            _order_count(order_count),
            _faction(0)
        {
            for (int i = 1; i < 5; i++)
            {
                _actions.set(i);
            }
            _actions.set(13);
            _actions.set(14);
            _actions.set(16);
            _actions.set(18);
            _actions.set(19);
            _actions.set(13 + BIT_OFFSET);
            _actions.set(11 + BIT_OFFSET);
            _actions.set(4 + BIT_OFFSET);
            _actions.set(6 + BIT_OFFSET);

        }
        ~Identity()
        {
        }
        // Identity(const Identity& other)
        //     : _name(other._name),
        //     _mrem(other._mrem), // 进行深度拷贝
        //     _history(other._history),
        //     _mode(other._mode),
        //     _actions(other._actions),
        //     _order_count(other._order_count), // 进行深度拷贝
        //     _faction(other._faction)
        // {
        // }
        //Identity(const Identity& other)
        //{
        //    _name = other._name;
        //    _mode = other._mode;
        //    _faction = other._faction;
        //    _history = other._history;
        //    RemberCache<Rember> tmp(other._mrem);
        //    _mrem = tmp;
        //    _actions = other._actions;
        //    if (!other._order_count.empty())
        //    {
        //        unordered_map<size_t, size_t> map_tmp = other._order_count;
        //        _order_count = map_tmp;
        //    }
        //    /*for (auto it = other._order_count.begin();it!=other._order_count.end();it++)
        //    {
        //        size_t key = it->first;
        //        size_t value = it->second;
        //        _order_count.insert({ key,value });
        //    }*/
        //}
        //Identity& operator=(const Identity& other)
        //{
        //    if (this == &other)
        //    {
        //        return *this; // 避免自我赋值
        //    }
        //    _name = other._name;
        //    RemberCache<Rember> tmp(other._mrem);
        //    _mrem = tmp;
        //    _history = other._history;
        //    _mode = other._mode;
        //    _actions = other._actions;
        //    _order_count.clear();
        //    if (!other._order_count.empty())
        //    {
        //        unordered_map<size_t, size_t> map_tmp = other._order_count;
        //        _order_count = map_tmp;
        //    }
        //   /* for (auto it = other._order_count.begin(); it != other._order_count.end(); it++)
        //    {
        //        size_t key = it->first;
        //        size_t value = it->second;
        //        _order_count.insert({ key,value });
        //    }*/
        //    _faction = other._faction;
        //    return *this;
        //}
        //依据模式值返回模式
        static size_t TestMode(double mode)
        {
            //1-34:非友好
            //34-67:正常
            //67-99:友好
            if (mode < 34)
            {
                return MOD_UNFRIENDLY;
            }
            else if (mode > 67)
            {
                return MOD_FRIENDLY;
            }
            else
            {
                return MOD_NORMAL;
            }
        }
        //设置模式值
        void SetMode(double m)
        {
            _mode = m;
            if (_mode >= 99)
            {
                _mode = 99;
            }
            if (_mode <= 1)
            {
                _mode = 1;
            }
        }
        //增加模式值
        void AddMode()
        {
            _mode++;
            if (_mode >= 100)
            {
                _mode = 100;
            }
        }
        //减少模式值
        void DelMode()
        {
            _mode--;
            if (_mode <= 0)
            {
                _mode = 0;
            }
        }
    };

    //输入信息
    struct Input
    {
        size_t _name;
        size_t _voice;
        size_t _pose;
        size_t _feel;
        Input()
            :_name(NAME_UNKNOW),
            _voice(0),
            _pose(0),
            _feel(0)
        {}
    };

    class StoreIdentity
    {
    public:
        //static  unordered_map<size_t, Identity> ReadIdentitiesFromFile( const std::string& filename)
        //{
        //    // 创建文件流对象
        //    std::ifstream file(filename, std::ios::binary);
        //    if (file.is_open())
        //    {
        //        //cout<<"文件正常打开"<<endl;
        //        // 读取 vector 的大小
        //        size_t size;
        //        file.read(reinterpret_cast<char*>(&size), sizeof(size_t));
        //        //cout<<"大小为:"<<size<<endl;
        //        // 逐个读取文件中的 Identity 结构体到 vector
        //        unordered_map<size_t, Identity> identitys;
        //        for (size_t i = 0; i < size; i++)
        //        {
        //            size_t key;
        //            Identity identity;
        //            file.read(reinterpret_cast<char*>(&key), sizeof(size_t));
        //            file.read(reinterpret_cast<char*>(&identity), sizeof(Identity));
        //            identitys.insert({ key,identity });
        //        }
        //        file.close();
        //        std::cout << "Identity map has been read from file: " << filename << std::endl;
        //        return identitys;
        //    }
        //    else
        //    {
        //        // 文件打开失败，可以进行错误处理
        //        std::cerr << "Failed to open file: " << filename << std::endl;
        //        return unordered_map<size_t, Identity>();
        //    }
        //    //cout<<"关闭文件成功"<<endl;
        //}
        // 
        // 
        //static bool WriteIdentityToFile(const std::unordered_map<size_t, Identity>& identityMap, const std::string& filename)
        //{
        //    // 创建文件流对象
        //    std::ofstream file(filename, std::ios::binary);
        //    if (file.is_open()) {
        //        size_t mapSize = identityMap.size();
        //        file.write(reinterpret_cast<const char*>(&mapSize), sizeof(size_t));
        //        for (const auto& pair : identityMap) {
        //            size_t key = pair.first;
        //            Identity identity = pair.second;
        //            file.write(reinterpret_cast<const char*>(&key), sizeof(size_t));
        //            file.write(reinterpret_cast<const char*>(&identity), sizeof(Identity));
        //        }
        //        file.close();
        //        std::cout << "Identity map has been written to file: " << filename << std::endl;
        //        return true;
        //    }
        //    else { 
        //        std::cerr << "Failed to open file: " << filename << std::endl;
        //        return false;
        //    }
        //}
        static bool WriteIdentityToFile(const std::unordered_map<size_t, Identity>& identityMap, const std::string& filename)
        {
            // 创建文件流对象
            std::ofstream file(filename, std::ios::binary);
            if (file.is_open()) {
                size_t mapSize = identityMap.size();
                file.write(reinterpret_cast<const char*>(&mapSize), sizeof(size_t));
                for (const auto& pair : identityMap) {
                    size_t key = pair.first;
                    Identity identity = pair.second;
                    size_t name = identity._name;
                    RemberCache<Rember> mrem = identity._mrem;
                    Rember hist = identity._history;
                    double mode = identity._mode;
                    std::bitset<64> acts = identity._actions;
                    unordered_map<size_t, size_t> order = identity._order_count;
                    size_t fact = identity._faction;
                    file.write(reinterpret_cast<const char*>(&key), sizeof(size_t));
                    file.write(reinterpret_cast<const char*>(&name), sizeof(size_t));
                    file.write(reinterpret_cast<const char*>(&mrem), sizeof(RemberCache<Rember>));
                    file.write(reinterpret_cast<const char*>(&hist), sizeof(Rember));
                    file.write(reinterpret_cast<const char*>(&mode), sizeof(double));
                    file.write(reinterpret_cast<const char*>(&acts), sizeof(bitset<64>));
                    file.write(reinterpret_cast<const char*>(&fact), sizeof(size_t));
                    //写入order
                    size_t order_size = order.size();
                    file.write(reinterpret_cast<const char*>(&order_size), sizeof(size_t));
                    for (const auto& e : order)
                    {
                        size_t k = e.first;
                        size_t v = e.second;
                        file.write(reinterpret_cast<const char*>(&k), sizeof(size_t));
                        file.write(reinterpret_cast<const char*>(&v), sizeof(size_t));
                    }
                    
                }
                file.close();
                std::cout << "Identity map has been written to file: " << filename << std::endl;
                return true;
            }
            else {
                std::cerr << "Failed to open file: " << filename << std::endl;
                return false;
            }
        }

        static  unordered_map<size_t, Identity> ReadIdentitiesFromFile(const std::string& filename)
        {
            // 创建文件流对象
            std::ifstream file(filename, std::ios::binary);
            if (file.is_open())
            {
                //cout<<"文件正常打开"<<endl;
                // 读取 vector 的大小
                size_t size;
                file.read(reinterpret_cast<char*>(&size), sizeof(size_t));
                //cout<<"大小为:"<<size<<endl;
                // 逐个读取文件中的 Identity 结构体到 vector
                unordered_map<size_t, Identity> identitys;
                for (size_t i = 0; i < size; i++)
                {
                    size_t key;
                    Identity identity;
                    size_t name ;
                    RemberCache<Rember> mrem ;
                    Rember hist ;
                    double mode ;
                    std::bitset<64> acts;
                    unordered_map<size_t, size_t> order ;
                    size_t fact;
                    file.read(reinterpret_cast<char*>(&key), sizeof(size_t));
                    file.read(reinterpret_cast<char*>(&name), sizeof(size_t));
                    file.read(reinterpret_cast<char*>(&mrem), sizeof(RemberCache<Rember>));
                    file.read(reinterpret_cast< char*>(&hist), sizeof(Rember));
                    file.read(reinterpret_cast< char*>(&mode), sizeof(double));
                    file.read(reinterpret_cast< char*>(&acts), sizeof(bitset<64>));
                    file.read(reinterpret_cast< char*>(&fact), sizeof(size_t));
                    identity._name = name;
                    identity._mrem = mrem;
                    identity._history = hist;
                    identity._mode = mode;
                    identity._actions = acts;
                    identity._order_count = order;
                    size_t order_size;
                    file.read(reinterpret_cast<char*>(&order_size), sizeof(size_t));
                    for (int j = 0; j < order_size; j++)
                    {
                        size_t k;
                        size_t v;
                        file.read(reinterpret_cast<char*>(&k), sizeof(size_t));
                        file.read(reinterpret_cast<char*>(&v), sizeof(size_t));
                        order.insert({ k,v });
                    }
                    identity._order_count = order;
                    identitys.insert({ key,identity });
                }
                file.close();
                std::cout << "Identity map has been read from file: " << filename << std::endl;
                return identitys;
            }
            else
            {
                // 文件打开失败，可以进行错误处理
                std::cerr << "Failed to open file: " << filename << std::endl;
                return unordered_map<size_t, Identity>();
            }
            //cout<<"关闭文件成功"<<endl;
        }
    };
    const static string DATA_PATH = "./RemberData/data.rem";//存储路径  
    class Desion 
    {
    public:
        unordered_map<size_t, Identity> _identitys;//存放交互人的身份信息
        RemberCache<Rember> _rembers;//近期记忆
        unordered_map<size_t, pair<size_t, double>> _executemap;//近期命令学习次数与几率的映射
        unordered_map<size_t, pair<size_t, double>> _ordermap;//近期命令执行次数与几率的映射
        Identity* _cur_iden;//永远记录当前当前交互者
        double _feedback = 0.5;//反馈信息
        double _avg_personality = 0;//平均性格
        double _f_persionlity = 0;//上一次性格
        double _s_persionlity = 0;//上上次性格
        double _range = 0.03;//学习速率--正常为0.03
        time_t _time = 0;//时间戳
        double _per_mode=0.0;
    public:
        Desion()
            :_identitys( unordered_map<size_t, Identity>()),
            _rembers(RemberCache<Rember>()),
            _executemap(unordered_map<size_t, pair<size_t, double>>()),
            _ordermap( unordered_map<size_t, pair<size_t, double>>()),
            _cur_iden(nullptr)
        {
            if (!LoadData())
            {
                logMessage(WARNING, "数据未加载成功！");
                // cout << "数据未加载成功" << endl;
            }
            _avg_personality = AvgRobotPerson();
        }
        ~Desion()
        {
            //析构之前需要先对本次交互的整个过程中的交互信息进行保存
            //但是保存之前将每个身份中本次互动命令的次数清零
            ClearFreq();
            StoreIdentity::WriteIdentityToFile(_identitys, DATA_PATH);
           
        }
        void AddPerMode()
        {
            _per_mode += 0.005;
            if (_per_mode >= 0.9)
                _per_mode == 0.9;
        }
        void DelPerMode()
        {
            _per_mode -= 0.005;
            if (_per_mode <= -0.9)
                _per_mode == -0.9;
        }
        double SetMode(double permode)
        {
            return permode*(5/0.9);
        }
        void TimeStart()
        {
            time(&_time);
        }
        void TimeEnd()
        {
            _time = 0;
        }
        bool TimeTest()
        {
            return _time == 0;
        }
        //平均个性
        double AvgRobotPerson()const
        {
            if (_identitys.size() == 0)
                return 50;
            double x = 0;
            for (auto& e : _identitys)
            {
                x += e.second._mode;
            }
            return x / _identitys.size();
        }
        //机器人个性
        double RobotPerson()const
        {
            //3:7
            //3:2:5
            if (_f_persionlity == 0)
            {
                return _avg_personality;
            }
            else if (_s_persionlity == 0)
            {
                return (0.3 * _f_persionlity + 0.7 * _avg_personality);
            }
            else
            {
                return (0.3 * _f_persionlity + 0.2 * _s_persionlity + 0.5 * _avg_personality);
            }
        }
        //学习速率
        double LearnRate(double mode)
        {
            if (Identity::TestMode(mode) == MOD_FRIENDLY)
            {
                return 0.04 + ((mode - 60) / 40) * 0.04;
            }
            else if (Identity::TestMode(mode) == MOD_UNFRIENDLY)
            {
                return 0.01 + ((mode - 0) / 40) * 0.02;
            }
            else
            {
                return 0.03 + ((mode - 40) / 20) * 0.01;
            }
        }

        //加载函数
        bool LoadData()
        {
            unordered_map<size_t, Identity> tmp;
            _identitys = StoreIdentity::ReadIdentitiesFromFile(DATA_PATH);
            if(_identitys.empty())
            {
                return false;
            }
            return true;
        }
        //次数清零函数
        void ClearFreq()
        {
            for (auto& e : (_identitys))
            {
                e.second._order_count.clear();
            }
        }
        //短时记忆清理函数
        void ClearRem()
        {
            time_t curtime = time(NULL);
            if ((curtime - _time) % FREE_TIME_TWO == 0)
            {
                for (auto& e : _executemap)
                {
                    e.second.first -= 2;
                    e.second.second -= 0.05;
                    if (e.second.first <= 0)
                    {
                        e.second.first = 0;
                    }
                    if (e.second.second <= 0.0)
                    {
                        e.second.second = 0.0;
                    }
                }
                for (auto& e : _identitys)
                {
                    for (auto& i : e.second._order_count)
                    {
                        if (i.second != 0)
                        {
                            i.second -= 2;
                            if (i.second <= 0)
                            {
                                i.second = 0;
                            }
                        }
                    }
                }
            }
        }

        //启动函数
        bool Start(Input& inp, Response* result)
        {
            if (inp._name == NAME_UNKNOW)
            {   
                if (_cur_iden != nullptr)
                {
                    _cur_iden->SetMode(_cur_iden->_mode+SetMode(_per_mode));
                    _per_mode = 0;
                }           
                if (TimeTest())
                    TimeStart();
                time_t cur_time = time(NULL);
                if (cur_time - _time == FREE_TIME_ONE)
                {
                    //空闲一分钟
                    result->_res = EMO_STUNNED;
                    result->_mode = ACTION_SITDOWN;
                    result->_expression += "短时间不交互,开始进入休息状态,";
                }
                else if (cur_time - _time == FREE_TIME_TWO)
                {
                    //空闲两分钟
                    result->_res = EMO_EXHAUSTED;
                    result->_mode = ACTION_GETDOWN;
                    result->_expression += "长时间不交互,进入休息状态,";
                }
                else
                {
                    //空闲两分钟以上
                    result->_res = EMO_EXHAUSTED;
                    result->_mode = ACTION_GETDOWN;
                    result->_expression += "长时间不交互,进入休息状态,开始清理部分短时记忆,";
                    ClearRem();
                }
                return false;
            }
            if (!TimeTest())
                TimeEnd();
            if (inp._voice == ORDER_AGREE
                || inp._pose == HANDER_OK || inp._voice == ORDER_DISAGREE
                || inp._pose == HANDER_DISAGREE)
            {
                result->_expression += "反馈信息,";
                //命令为 反馈信息
                if (!_rembers .empty()&& _rembers.is_last_rember())
                {
                    //具备上次记忆
                    Rember lastrem = _rembers.get_first();
                    if (inp._voice == ORDER_AGREE
                        || inp._pose == HANDER_OK)
                    {
                        lastrem.AddProbility(P_RANGE);
                        result->_res = EMO_HAPPY;
                        result->_mode = ACTION_INIT;
                        result->_expression += "正确反馈,";
                        _feedback += P_RANGE;
                        if (_feedback >= 1.0)
                        {
                            _feedback = 1.0;
                        }
                    }
                    else
                    {
                        lastrem.DelProbility(P_RANGE);
                        result->_res = EMO_GRIEVANCE;
                        result->_mode = ACTION_INIT;
                        result->_expression += "错误反馈,";
                        _feedback -= P_RANGE;
                        if (_feedback <= 0.0)
                        {
                            _feedback = 0.0;
                        }
                    }
                    return true;
                }
            }
            //命令不为反馈信息，或者不具备上次记忆，均为新的交互
            return Interaction(inp, result);
        }
        //交互函数
        //status: 0--语音  1--手势

        bool Interaction(Input& inp, Response* result)
        {
            if (_cur_iden==nullptr ||inp._name != _cur_iden->_name)
            {
                //进入到这里说明切换交互者了
                //更新个性
                if (_cur_iden != nullptr)
                {
                    _cur_iden->SetMode(_cur_iden->_mode+SetMode(_per_mode));
                    _s_persionlity = _f_persionlity;
                    _f_persionlity = _cur_iden->_mode;
                }
                _avg_personality = AvgRobotPerson();//切换身份
                auto pos = FindIdentity(inp._name);
                if (!pos.second)
                {
                    //没找到，插入进去
                    Identity cur;
                    cur._mode = RobotPerson();//新用户的交互方式由机器人的个性决定
                    //新的交互者的学习效率由mode决定
                    // _range = LearnRate(cur.TestMode());
                    //学习效率与mode的大小有关
                    _range = LearnRate(cur._mode);
                    cur._name = inp._name;
                    _identitys.insert({ inp._name,cur });
                }
                
                // cur_iden 为当前交互者
                auto res = FindIdentity(inp._name);
                _cur_iden = &res.first->second;

                //交互反馈清零
                _per_mode = 0.0;
            }
            //1.判断交互命令是否合法
            if (inp._voice == ORDER_UNKNOW && inp._pose == HANDER_UNKNOW)
            {
                //不合法
                result->_mode = ACTION_UNKNOW;
                result->_res = EMO_UNKNOW;
                result->_expression += "命令无效,";
                return false;
            }
            //合法的命令
            //友好状态--100%执行
            //正常状态--70%执行
            //非友好状态--50%执行
            if (Identity::TestMode(_cur_iden->_mode) == MOD_FRIENDLY)
            {
                result->_expression += "友好模式,";
                ExecuteCommond(_cur_iden, inp, result);
            }
            else if (Identity::TestMode(_cur_iden->_mode) == MOD_NORMAL)
            {
                result->_expression += "正常模式,";
                bool res = ExecutionProbability(0.9);
                if (res)
                {
                    ExecuteCommond(_cur_iden, inp, result);
                }
                else {
                    result->_expression += "忽略命令,";
                    if (!_rembers.empty())
                    {
                        result->_res = _rembers.get_first()._emotion;
                    }
                    return false;
                }
            }
            else
            {
                result->_expression += "非友好模式,";
                bool res = ExecutionProbability(0.8);
                if (res)
                {
                    ExecuteCommond(_cur_iden, inp, result);
                }
                else {
                    result->_expression += "忽略命令,";
                    if (!_rembers.empty())
                    {
                        result->_res = _rembers.get_first()._emotion;
                    }
                    return false;
                }
            }
            return true;
        }
         //执行命令函数
        void ExecuteCommond(Identity* cur_iden, Input& inp, Response* result)
        {
            pair<size_t, double> prob;
            bool v = false;
            bool p = false;
            if (inp._voice != ORDER_UNKNOW)
            {   //有语音命令
                v = true; p = false;
                if (cur_iden->_actions.test(inp._voice))
                {
                    //命令动作学会
                    prob = PerformAction(inp._voice, STA_VOICE, result, *cur_iden);
                }
                else if (cur_iden->_actions.test(inp._pose + BIT_OFFSET))
                {
                    //命令动作未学会，手势学会
                    p = true;
                    prob = PerformAction(inp._pose, STA_POSE, result, *cur_iden);
                }
                else
                {   //新的动作需要学习
                    prob = LearnAction(inp._voice, STA_VOICE, result, *cur_iden);
                }
            }
            else
            {
                v = false, p = true;
                //仅有手势
                if (cur_iden->_actions.test(inp._pose + BIT_OFFSET))
                {
                    //动作学会
                    prob = PerformAction(inp._pose, STA_POSE, result, *cur_iden);
                }
                else
                {
                    //动作未学会
                    prob = LearnAction(inp._pose, STA_POSE, result, *cur_iden);
                }
            }

            if (prob.second == 1.0)
            {
                //命令学会--更新
                if ((p && v) || (!v && p))
                {
                    //pose
                    if (!cur_iden->_actions.test(inp._pose + BIT_OFFSET))
                    {
                        cur_iden->_actions.set(inp._pose + BIT_OFFSET);
                        //命令已经学会，应该从_ordermap中删除
                        _ordermap.erase(inp._pose << BIT_POS);
                    }
                }
                else if (v && !p)
                {
                    //voice
                    if (!cur_iden->_actions.test(inp._voice))
                    {
                        cur_iden->_actions.set(inp._voice);
                        //命令已经学会，应该从_ordermap中删除
                        _ordermap.erase(inp._voice);
                    }
                }

            }

        }//执行需要考虑表情和动作,返回动作执行次数和执行几率

        //学习动作函数
        pair<size_t, double> LearnAction(size_t commond, size_t status, Response* result, Identity& id)
        {
            size_t commond_pos = commond;//命令存储键值
            if (status == STA_POSE)
            {
                //手势命令
                commond_pos = commond << BIT_POS;
            }
            result->_expression += "学习命令,";
            size_t mode = Identity::TestMode(id._mode);
            if (_ordermap.find(commond_pos) == _ordermap.end())
            {

                //第一次执行，没有执行次数
                ((_ordermap)[commond_pos].first)++;//次数递增

                size_t act = SetupAction(commond, status);
                size_t emo = SetupEmotion(commond, status, mode);
                //查看之前的交互行为中是否多次执行过此动作
                if (_executemap.find(commond_pos) != _executemap.end())
                {
                    //前面的交互行为中多次执行过这个命令
                    //将会加快学习的过程
                    if ((_executemap)[commond_pos].first >= 10)
                    {
                        //学习效果将大幅提升
                        //_range = 0.1;//
                        ((_ordermap)[commond_pos].second) = 0.3;
                        result->_expression = "之前多次执行过该命令,学习更快,";
                    }
                }
                else
                {
                    //先查看之前是否学过但没学会此命令
                    if (id._mrem.get_first()._action == act && id._mrem.get_first()._emotion == emo)
                    {
                        ((_ordermap)[commond_pos].second) = id._mrem.get_first()._probability;
                        result->_expression = "之前的记忆中学过该命令,";
                    }
                    else if (id._mrem.get_second()._action == act && id._mrem.get_second()._emotion == emo)
                    {
                        ((_ordermap)[commond_pos].second) = id._mrem.get_second()._probability;
                        result->_expression = "之前的记忆中学过该命令,";
                    }
                    else {
                        //之前短时记忆中没有学过
                        ((_ordermap)[commond_pos].second) = _range;
                    }
                }

                _feedback = 0.5;
                bool is_execute = ExecutionProbability(((_ordermap)[commond_pos].second));
                if (is_execute)
                {
                    //执行动作
                    result->_mode = act;
                    if (result->_mode == 100)
                    {
                        result->_expression = "命令出错!";
                        result->_mode = ACTION_UNKNOW;
                        result->_res = EMO_UNKNOW;
                        return pair<size_t, double>();
                    }
                    result->_res = SetupEmotion(commond, status, mode);
                    result->_expression += "第一次接收的命令正在执行,";
                }
                else
                {
                    //不执行动作
                    result->_res = EMO_NORMAL_2;
                    result->_expression += "第一次接收的命令未执行,";
                }


            }
            //正常状态下：0.03  0.5/0.05 = 10次
            //else if (((*_ordermap)[commond_pos].second > 0.1 && (*_ordermap)[commond_pos].second <= 0.5) || (_ordermap->at(commond_pos).first >= 1 && _ordermap->at(commond_pos).first <= 10))
            else if (((_ordermap)[commond_pos].second < 0.6))
            {
                //疑惑状态，尝试执行
                ((_ordermap)[commond_pos].first)++;//次数递增
                ((_ordermap)[commond_pos].second) += _range;
                bool is_execute = ExecutionProbability(((_ordermap)[commond_pos].second));
                if (is_execute)
                {
                    //执行动作
                    result->_mode = SetupAction(commond, status);
                    if (result->_mode == 100)
                    {
                        result->_expression = "命令出错!";
                        result->_mode = ACTION_UNKNOW;
                        result->_res = EMO_UNKNOW;
                        return pair<size_t, double>();
                    }
                    result->_res = EMO_CONFUSION;
                    result->_expression += "尝试执行命令,";
                }
                else
                {
                    //不执行动作
                    result->_res = ExecutionProbability(0.5) ? EMO_CONFUSION : EMO_NORMAL_2;
                    result->_expression += "尝试执行命令,但是未执行,";
                }
            }
            //else if (((*_ordermap)[commond_pos].second >= 0.6 && (*_ordermap)[commond_pos].second <= 0.9) || (_ordermap->at(commond_pos).first > 10 && _ordermap->at(commond_pos).first < 20))
            else if (((_ordermap)[commond_pos].second >= 0.6 && (_ordermap)[commond_pos].second < 0.9))

            {   //感兴趣，大概率执行
                ((_ordermap)[commond_pos].first)++;//次数递增
                ((_ordermap)[commond_pos].second) += _range;
                bool is_execute = ExecutionProbability(((_ordermap)[commond_pos].second));
                if (is_execute)
                {
                    //执行动作
                    result->_mode = SetupAction(commond, status);
                    if (result->_mode == 100)
                    {
                        result->_expression = "命令出错!";
                        result->_mode = ACTION_UNKNOW;
                        result->_res = EMO_UNKNOW;
                        return pair<size_t, double>();
                    }
                    result->_res = SetupEmotion(commond, status, mode);
                    result->_expression += "对命令感兴趣,执行命令,";
                }
                else
                {
                    //不执行动作
                    result->_mode =
                        result->_res = ExecutionProbability(0.5) ? SetupEmotion(commond, status, mode) : EMO_NORMAL_2;
                    result->_expression += "对命令感兴趣,但是未执行,";

                }
            }
            else {
                //学会了动作

                ((_ordermap)[commond_pos].first)++;//次数递增
                ((_ordermap)[commond_pos].second) += _range;

                // PerformAction(commond_pos,status,result);
                _feedback = 1.0;
                result->_mode = SetupAction(commond, status);
                if (result->_mode == 100)
                {
                    result->_expression = "命令出错!";
                    result->_mode = ACTION_UNKNOW;
                    result->_res = EMO_UNKNOW;
                    return pair<size_t, double>();
                }
                result->_res = SetupEmotion(commond, status, mode);
                result->_expression += "命令基本学会且执行,";
                if ((_ordermap)[commond_pos].second < 1.0)
                    result->_expression += "命令基本学会且执行,";
                if ((_ordermap)[commond_pos].second >= 1.0)
                {
                    ((_ordermap)[commond_pos].second) = 1.0;
                    ((_ordermap)[commond_pos].first) = 0;
                    id._order_count[commond_pos] = 0;
                    result->_expression += "命令已经学会且执行,次数清零,";

                }
            }

            //形成记忆
            Rember rem;
            rem._action = result->_mode;
            rem._emotion = result->_res;
            if (rem._action == ACTION_SHAKEHAND || rem._action == ACTION_BUMPFISR
                || rem._action == ACTION_REVOLVE_L || rem._action == ACTION_REVOLVE_R
                || rem._action == ACTION_DANCE || rem._emotion == EMO_HAPPY
                || rem._emotion == EMO_LOVE || rem._emotion == EMO_ENJOY
                || ((status == STA_VOICE) && (commond == ORDER_NEAR || commond ==ORDER_BUMPFISR 
                    || commond == ORDER_SHAKEHAND || commond == ORDER_DANCE))
                ||((status == STA_POSE)&&(commond == HANDER_FIST_BUMPS || commond == HANDER_AGREE
                    || commond == HANDER_LOVE || commond == HANDER_THUM_UP)))
            {
                AddPerMode();
                
            }
            if (rem._action == ACTION_RUN || rem._action == ACTION_BACK
                || rem._action == ACTION_UPDOWM || rem._action == ACTION_FRONTBACK
                || rem._emotion == EMO_PAIN
                || rem._emotion == EMO_VERTIGO || rem._emotion == EMO_ANGRY || rem._emotion == EMO_SAD
                || ((status == STA_POSE) && (commond == HANDER_PINK_UP || commond == HANDER_FUCK 
                    || commond == HANDER_SHOOT ))
                || ((status == STA_VOICE) && (commond == ORDER_BACK)))
            {
                DelPerMode();
              
            }
            rem._model = Identity::TestMode(id._mode);
            rem._name = id._name;
            rem._probability = ((_ordermap)[commond_pos].second);
            if (((_ordermap)[commond_pos].second) != 1.0)
            {
                if (((_ordermap)[commond_pos].second) <= 0.2)
                {
                    rem._attitude = ATT_NORMAL;
                }
                else if (((_ordermap)[commond_pos].second) < 0.6)
                {
                    rem._attitude = ATT_PUZZLE;
                }
                else
                {
                    rem._attitude = ATT_INTEREST;
                }
            }
            else
            {
                if (((_ordermap)[commond_pos].first) < 15)
                {
                    rem._attitude = ATT_FAVORITE;
                }
                else
                {
                    rem._attitude = ATT_INDIFFERENT;
                }
            }
            _rembers.Push(rem);
            id._order_count[commond_pos]++;
            if (id._mrem.history_inc().first && id._mrem.get_first() == rem)
            {
                id._history = id._mrem.get_first();
            }
            id._mrem.Push(rem);

            return { ((_ordermap)[commond_pos].first),((_ordermap)[commond_pos].second) };
        }
        //执行动作函数
        pair<size_t, double> PerformAction(size_t commond, size_t status, Response* result, Identity& id)
        {
            //来到这里，表明此条命令已经学会
            size_t commond_pos = commond;//命令存储键值
            if (status == STA_POSE)
            {
                //手势命令
                commond_pos = commond << BIT_POS;
            }
            result->_expression += "执行命令,";
            size_t fre = id._order_count[commond_pos];//次数
            size_t mode = Identity::TestMode(id._mode);//模式
            //命令为停止--直接执行--不进入记忆
            if (status == STA_VOICE)
            {
                if (commond == ORDER_STOP)
                {
                    result->_mode = SetupAction(commond, status);
                    result->_res = SetupEmotion(commond, status, mode);
                    result->_expression += "停止命令，动作停止执行";
                    return { 0,1.0 };
                }
            }

            double cur_odds = 0.0;//本次执行几率
            //设置本应该执行的动作和表情
            size_t emo = SetupEmotion(commond, status, mode);//执行表情
            size_t act = SetupAction(commond, status);//执行动作

            size_t att = ATT_NORMAL;

            //模式决定执行频率
            if (mode == MOD_NORMAL)
            {
                result->_expression += "正常模式,";
                if (fre >= 0 && fre < 3)
                {
                    cur_odds = 0.85;//本次执行几率
                    result->_expression += "兴趣,";
                    att = ATT_INTEREST;
                }
                else
                {
                    cur_odds = SetOdds(fre, mode);
                    if (cur_odds >= 0.8)
                    {
                        result->_expression += "喜欢,";
                        att = ATT_FAVORITE;
                    }
                    else if (cur_odds >= 0.6)
                    {
                        result->_expression += "无语,";
                        att = ATT_SPEECHLESS;
                        emo = EMO_SPEECHLESS;
                    }
                    else if (cur_odds >= 0.4)
                    {
                        result->_expression += "疲惫,";
                        att = ATT_EXHAUSTED;
                        emo = EMO_EXHAUSTED;
                    }
                    else
                    {
                        result->_expression += "冷漠,";
                        bool flag = ExecutionProbability(0.5);
                        if (flag)
                        {
                            emo = EMO_INDIFFERENT;
                        }
                        else
                        {
                            emo = EMO_NORMAL_1;
                        }

                        att = ATT_INDIFFERENT;
                    }
                }
            }
            else if (mode == MOD_FRIENDLY)
            {
                result->_expression += "友好模式,";
                cur_odds = SetOdds(fre, mode);
                if (cur_odds >= 0.8)
                {
                    result->_expression += "喜欢,";
                    att = ATT_FAVORITE;
                }
                else if(cur_odds >= 0.7)
                {
                    result->_expression += "无语,";
                    att = ATT_SPEECHLESS;
                    emo = EMO_SPEECHLESS;
                }
                else if (cur_odds > 0.5)
                {
                    result->_expression += "疲惫,";
                    att = ATT_EXHAUSTED;
                    emo = EMO_EXHAUSTED;
                }
                else
                {
                    result->_expression += "冷漠,";
                    bool flag = ExecutionProbability(0.4);
                    if (flag)
                    {

                        emo = EMO_INDIFFERENT;
                    }
                    else
                    {
                        emo = EMO_NORMAL_1;
                    }
                    att = ATT_INDIFFERENT;
                }
            }
            else
            {
                result->_expression += "非友好模式,";
                if (fre >= 0 && fre < 3)
                {
                    cur_odds = 0.8;//本次执行几率
                    result->_expression += "兴趣,";
                    att = ATT_INTEREST;
                }
                else
                {
                    cur_odds = SetOdds(fre, mode);
                    if (cur_odds >= 0.65)
                    {
                        result->_expression += "喜欢,";
                        att = ATT_FAVORITE;
                    }
                    else if (cur_odds >= 0.5)
                    {

                       result->_expression += "无语,";
                       att = ATT_SPEECHLESS;
                       emo = EMO_SPEECHLESS;
                    }
                    else if (cur_odds >= 0.4)
                    {
                        result->_expression += "疲惫,";
                        att = ATT_EXHAUSTED;
                        emo = EMO_EXHAUSTED;
                    }
                    else
                    {
                        result->_expression += "冷漠,";
                        bool flag = ExecutionProbability(0.6);
                        if (flag)
                        {

                            emo = EMO_INDIFFERENT;
                        }
                        else
                        {
                            emo = EMO_NORMAL_1;
                        }
                        att = ATT_INDIFFERENT;
                    }
                }
            }
            double odds = 0.0;//最终执行几率
            //查看上次执行的动作
            if (id._mrem.get_first()._action == act && id._mrem.get_second()._action == act && id._mrem.get_first()._emotion == emo && id._mrem.get_second()._emotion == emo)
            {
                double last_odds = id._mrem.get_first()._probability;
                double second_last_odds = id._mrem.get_second()._probability;
                double history_odds = 0.0;
                if (id._history._action == act && id._history._emotion == emo)
                {
                    history_odds = id._history._probability;
                }
                odds = cur_odds * 0.6 + 0.25 * last_odds + 0.1 * second_last_odds + 0.05 * history_odds;
            }
            else if (id._mrem.get_first()._action == act && id._mrem.get_first()._emotion == emo)
            {
                double last_odds = id._mrem.get_first()._probability;
                double history_odds = 0.0;
                if (id._history._action == act && id._history._emotion == emo)
                {
                    history_odds = id._history._probability;
                }
                odds = cur_odds * 0.7 + 0.25 * last_odds + 0.05 * history_odds;
            }
            else if (id._mrem.get_second()._action == act && id._mrem.get_second()._emotion == emo)
            {
                double second_last_odds = id._mrem.get_second()._probability;
                double history_odds = 0.0;
                if (id._history._action == act && id._history._emotion == emo)
                {
                    history_odds = id._history._probability;
                }
                odds = cur_odds * 0.7 + 0.2 * second_last_odds + 0.1 * history_odds;
            }
            else
            {
                double history_odds = 0.0;
                if (id._history._action == act && id._history._emotion == emo)
                {
                    history_odds = id._history._probability;
                }
                odds = cur_odds * 0.9 + 0.1 * history_odds;

            }
            result->_res = emo;//设置返回表情
            bool exe = ExecutionProbability(odds);
            if (exe)
            {
                //依据执行几率设置执行动作
                result->_mode = act;
                result->_expression += "此次命令已执行,几率为: ";
                result->_expression += to_string(odds);
                //只有执行了次数才会增加
                ((_executemap)[commond_pos].first)++;//次数增加
            }
            else
            {
                if (act == _rembers.get_first()._action)
                {
                    result->_mode = ACTION_STOP;
                    result->_expression += "此次命令忽略，不执行动作,";
                }
                else
                {
                    result->_mode = _rembers.get_first()._action;//此次命令忽略，维持上一次命令
                    result->_expression += "此次命令忽略，维持上一次命令,";
                }

                //未执行动作，次数不增加
            }

            //此次动作执行完毕
            ((_executemap)[commond_pos].second) = odds;//几率更新

            //形成记忆
            Rember rem;
            rem._action = result->_mode;
            rem._emotion = result->_res;
            if (rem._action == ACTION_SHAKEHAND || rem._action == ACTION_BUMPFISR
                || rem._action == ACTION_REVOLVE_L || rem._action == ACTION_REVOLVE_R
                || rem._action == ACTION_DANCE || rem._emotion == EMO_HAPPY
                || rem._emotion == EMO_LOVE || rem._emotion == EMO_ENJOY
                || ((status == STA_VOICE) && (commond == ORDER_NEAR || commond ==ORDER_BUMPFISR 
                    || commond == ORDER_SHAKEHAND || commond == ORDER_DANCE))
                ||((status == STA_POSE)&&(commond == HANDER_FIST_BUMPS || commond == HANDER_AGREE
                    || commond == HANDER_LOVE || commond == HANDER_THUM_UP)))
            {
                AddPerMode();
                
            }
            if (rem._action == ACTION_RUN || rem._action == ACTION_BACK
                || rem._action == ACTION_UPDOWM || rem._action == ACTION_FRONTBACK
                || rem._emotion == EMO_PAIN
                || rem._emotion == EMO_VERTIGO || rem._emotion == EMO_ANGRY || rem._emotion == EMO_SAD
                || ((status == STA_POSE) && (commond == HANDER_PINK_UP || commond == HANDER_FUCK 
                    || commond == HANDER_SHOOT ))
                || ((status == STA_VOICE) && (commond == ORDER_BACK)))
            {
                DelPerMode();
              
            }
            rem._model = Identity::TestMode(id._mode);
            rem._name = id._name;
            rem._probability = ((_executemap)[commond_pos].second);
            rem._attitude = att;
            _rembers.Push(rem);
            id._order_count[commond_pos]++;
            if (id._mrem.history_inc().first && id._mrem.get_first() == rem)
            {
                id._history = id._mrem.get_first();
            }
            id._mrem.Push(rem);
            return { ((_executemap)[commond_pos].first), ((_executemap)[commond_pos].second) };
        }

        double SetOdds(size_t fre, size_t mode)
        {
            if (mode == MOD_NORMAL)//正常
            {
                if (fre >= 3 && fre <= 15)
                {
                    
                    return 1 - (fre - 3) * 0.03;
                }
                else if (fre > 15 && fre < 25)
                {
                    return 0.85 - (fre - 15) * 0.05;
                }
                else
                {
                    return 0.6 - (fre - 25) * 0.02;
                }
            }
            else if (mode == MOD_FRIENDLY)//友好
            {
                if (fre >= 0 && fre <= 20)
                {
                    return 1 - (fre - 0) * 0.02;
                }
                else if (fre > 20 && fre <= 30)
                {
                    return 0.8 - (fre - 20) * 0.03;
                }
                else
                {
                    return 0.5 - (fre - 30) * 0.01;
                }
            }
            else//非友好
            {
                if (fre >= 3 && fre <= 20)
                    return 1 - (fre - 3) * 0.035;
                else
                    return 0.65 - (fre - 20) * 0.01;
            }
        }
        //设置返回表情
        size_t SetupEmotion(size_t commond, size_t status, size_t mode)
        {
            size_t res = 0;
            if (status == STA_VOICE)
            {
                if (mode == MOD_NORMAL)
                {
                    //正常
                    switch (commond)
                    {
                    case ORDER_LEFT:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_RIGHT:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_TURNBACK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_STOP:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_BACK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_HALLO:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_NEAR:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_WALK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_RUN:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_REVOLVE:
                        res = EMO_VERTIGO;
                        break;
                    case ORDER_PUSHUP:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_JUMP:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_DANCE:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_MOONWALK:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_SITDOWN:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_GETDOWN:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_SHAKEHAND:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_BUMPFISR:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_SLID:
                        res = EMO_HAPPY;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
                else if (mode == MOD_FRIENDLY)
                {
                    //友好
                    switch (commond)
                    {
                    case ORDER_LEFT:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_RIGHT:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_TURNBACK:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_STOP:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_BACK:
                        res = EMO_CONFUSION;
                        break;
                    case ORDER_HALLO:
                        res = EMO_LOVE;
                        break;
                    case ORDER_NEAR:
                        res = EMO_LOVE;
                        break;
                    case ORDER_WALK:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_RUN:
                        res = EMO_CONFUSION;
                        break;
                    case ORDER_REVOLVE:
                        res = EMO_VERTIGO;
                        break;
                    case ORDER_PUSHUP:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_JUMP:
                        res = EMO_HAPPY;
                        break;
                    case ORDER_DANCE:
                        res = EMO_LOVE;
                        break;
                    case ORDER_MOONWALK:
                        res = EMO_LOVE;
                        break;
                    case ORDER_SITDOWN:
                        res = EMO_EXPECT;
                        break;
                    case ORDER_GETDOWN:
                        res = EMO_ENJOY;
                        break;
                    case ORDER_SHAKEHAND:
                        res = EMO_EXPECT;
                        break;
                    case ORDER_BUMPFISR:
                        res = EMO_ENJOY;
                        break;
                    case ORDER_SLID:
                        res = EMO_LOVE;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
                else
                {
                    //非友好
                    switch (commond)
                    {
                    case ORDER_LEFT:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_RIGHT:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_TURNBACK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_STOP:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_BACK:
                        res = EMO_GRIEVANCE;
                        break;
                    case ORDER_HALLO:
                        res = EMO_INDIFFERENT;
                        break;
                    case ORDER_NEAR:
                        res = EMO_CONFUSION;
                        break;
                    case ORDER_WALK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_RUN:
                        res = EMO_PAIN;
                        break;
                    case ORDER_REVOLVE:
                        res = EMO_VERTIGO;
                        break;
                    case ORDER_PUSHUP:
                        res = EMO_PAIN;
                        break;
                    case ORDER_JUMP:
                        res = EMO_INDIFFERENT;
                        break;
                    case ORDER_DANCE:
                        res = EMO_SPEECHLESS;
                        break;
                    case ORDER_MOONWALK:
                        res = EMO_NORMAL_2;
                        break;
                    case ORDER_SITDOWN:
                        res = EMO_CONFUSION;
                        break;
                    case ORDER_GETDOWN:
                        res = EMO_CONFUSION;
                        break;
                    case ORDER_SHAKEHAND:
                        res = EMO_STUNNED;
                        break;
                    case ORDER_BUMPFISR:
                        res = EMO_STUNNED;
                        break;
                    case ORDER_SLID:
                        res = EMO_SPEECHLESS;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
            }
            else
            {
                if (mode == MOD_NORMAL)
                {
                    //正常
                    switch (commond)
                    {
                    case HANDER_FIST_BUMPS:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_ANGRY:
                        res = EMO_GRIEVANCE;
                        break;
                    case HANDER_HAPPY:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_SITDOWN:
                        res = EMO_NORMAL_2;
                        break;
                    case HANDER_HELLO:
                        res = EMO_NORMAL_2;
                        break;
                    case HANDER_AGREE:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_SHOOT:
                        res = EMO_GRIEVANCE;
                        break;
                    case HANDER_BYE:
                        res = EMO_NORMAL_2;
                        break;
                    case HANDER_PINK_UP:
                        res = EMO_STUNNED;
                        break;
                    case HANDER_FUCK:
                        res = EMO_ANGRY;
                        break;
                    case HANDER_THUM_UP:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_LOVE:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_ROLL:
                        res = EMO_HAPPY;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
                else if (mode == MOD_FRIENDLY)
                {
                    //友好
                    switch (commond)
                    {
                    case HANDER_FIST_BUMPS:
                        res = EMO_ENJOY;
                        break;
                    case HANDER_ANGRY:
                        res = EMO_STUNNED;
                        break;
                    case HANDER_HAPPY:
                        res = EMO_LOVE;
                        break;
                    case HANDER_SITDOWN:
                        res = EMO_EXPECT;
                        break;
                    case HANDER_HELLO:
                        res = EMO_LOVE;
                        break;
                    case HANDER_AGREE:
                        res = EMO_LOVE;
                        break;
                    case HANDER_SHOOT:
                        res = EMO_GRIEVANCE;
                        break;
                    case HANDER_BYE:
                        res = EMO_GRIEVANCE;
                        break;
                    case HANDER_PINK_UP:
                        res = EMO_CONFUSION;
                        break;
                    case HANDER_FUCK:
                        res = EMO_ANGRY;
                        break;
                    case HANDER_THUM_UP:
                        res = EMO_LOVE;
                        break;
                    case HANDER_LOVE:
                        res = EMO_LOVE;
                        break;
                    case HANDER_ROLL:
                        res = EMO_LOVE;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
                else
                {
                    //非友好
                    switch (commond)
                    {
                    case HANDER_FIST_BUMPS:
                        res = EMO_STUNNED;
                        break;
                    case HANDER_ANGRY:
                        res = EMO_GRIEVANCE;
                        break;
                    case HANDER_HAPPY:
                        res = EMO_INDIFFERENT;
                        break;
                    case HANDER_SITDOWN:
                        res = EMO_CONFUSION;
                        break;
                    case HANDER_HELLO:
                        res = EMO_INDIFFERENT;
                        break;
                    case HANDER_AGREE:
                        res = EMO_STUNNED;
                        break;
                    case HANDER_SHOOT:
                        res = EMO_SAD;
                        break;
                    case HANDER_BYE:
                        res = EMO_INDIFFERENT;
                        break;
                    case HANDER_PINK_UP:
                        res = EMO_ANGRY;
                        break;
                    case HANDER_FUCK:
                        res = EMO_ANGRY;
                        break;
                    case HANDER_THUM_UP:
                        res = EMO_HAPPY;
                        break;
                    case HANDER_LOVE:
                        res = EMO_STUNNED;
                        break;
                    case HANDER_ROLL:
                        res = EMO_SPEECHLESS;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
            }
            return res;
        }

        //设置返回动作
        size_t SetupAction(size_t commond, size_t status)
        {
            bool flag = ExecutionProbability(0.5);
            size_t res = 0;
            if (status == STA_VOICE)
            {
                if (_feedback != 1.0)
                {
                    if (commond == ORDER_WALK || commond == ORDER_RUN)
                    {
                        //walk or run
                        return ExecutionProbability(_feedback) ? ACTION_WALK : ACTION_RUN;
                    }
                    else if (commond == ORDER_REVOLVE)
                    {
                        //revolve_r or revolve_l
                        return ExecutionProbability(_feedback) ? ACTION_REVOLVE_L : ACTION_REVOLVE_R;
                    }
                    else if (commond == ORDER_PUSHUP || commond == ORDER_JUMP)
                    {
                        //push_up or up_down
                        return ExecutionProbability(_feedback) ? ACTION_PUSHUP : ACTION_UPDOWM;
                    }
                    else if (commond == ORDER_DANCE || commond == ORDER_MOONWALK)
                    {
                        //dance or moon_walk
                        return ExecutionProbability(_feedback) ? ACTION_DANCE : ACTION_MOONWALK_L;
                    }
                    else if (commond == ORDER_SITDOWN || commond == ORDER_GETDOWN)
                    {
                        // sitdown or gitdown
                        return ExecutionProbability(_feedback) ? ACTION_SITDOWN : ACTION_GETDOWN;
                    }
                    else if (commond == ORDER_BUMPFISR || commond == ORDER_SHAKEHAND)
                    {
                        //bumpfisr or shakehand
                        return ExecutionProbability(_feedback) ? ACTION_BUMPFISR : ACTION_SHAKEHAND;
                    }
                }
                else {

                    switch (commond)
                    {
                    case ORDER_LEFT:
                        res = ACTION_TURN_L;
                        break;
                    case ORDER_RIGHT:
                        res = ACTION_TURN_R;
                        break;
                    case ORDER_STOP:
                        res = ACTION_STOP;
                        break;
                    case ORDER_BACK:
                        res = ACTION_BACK;
                        break;
                    case ORDER_HALLO:
                        res = ACTION_HALLO;
                        break;
                    case ORDER_NEAR:
                        res = ACTION_NEAR;
                        break;
                    case ORDER_WALK:
                        res = ACTION_WALK;
                        break;
                    case ORDER_RUN:
                        res = ACTION_RUN;
                        break;
                    case ORDER_REVOLVE:
                        res = flag ? ACTION_REVOLVE_L : ACTION_REVOLVE_R;
                        break;
                    case ORDER_PUSHUP:
                        res = ACTION_PUSHUP;
                        break;
                    case ORDER_JUMP:
                        res = ACTION_UPDOWM;
                        break;
                    case ORDER_DANCE:
                        res = ACTION_DANCE;
                        break;
                    case ORDER_MOONWALK:
                        res = ACTION_MOONWALK_L;
                        break;
                    case ORDER_SITDOWN:
                        res = ACTION_SITDOWN;
                        break;
                    case ORDER_GETDOWN:
                        res = ACTION_GETDOWN;
                        break;
                    case ORDER_SHAKEHAND:
                        res = ACTION_SHAKEHAND;
                        break;
                    case ORDER_BUMPFISR:
                        res = ACTION_BUMPFISR;
                        break;
                    case ORDER_SLID:
                        res = ACTION_FRONTBACK;
                        break;
                    case ORDER_TURNBACK:
                        res = ACTION_TURNBACK;
                        break;
                    default:
                        res = 100;
                        cout << "指令出错！" << endl;
                        break;
                    }
                }
            }
            else
            {
                switch (commond)
                {
                case HANDER_FIST_BUMPS:
                    res = ACTION_BUMPFISR;
                    break;
                case HANDER_ANGRY:
                    res = ACTION_GETDOWN;
                    break;
                case HANDER_HAPPY:
                    res = ACTION_SITDOWN;
                    break;
                case HANDER_SITDOWN:
                    res = ACTION_SITDOWN;
                    break;
                case HANDER_HELLO:
                    res = ACTION_HALLO;
                    break;
                case HANDER_AGREE:
                    res = ACTION_INIT;
                    break;
                case HANDER_SHOOT:
                    res = ACTION_GETDOWN;
                    break;
                case HANDER_BYE:
                    res = ACTION_HALLO;
                    break;
                case HANDER_PINK_UP:
                    res = ACTION_INIT;
                    break;
                case HANDER_THUM_UP:
                    res = ACTION_INIT;
                    break;
                case HANDER_FUCK:
                    res = ACTION_INIT;
                    break;
                case HANDER_LOVE:
                    res = ACTION_SITDOWN;
                    break;
                case HANDER_ROLL:
                    res = ACTION_DANCE;
                    break;
                default:
                    res = 100;
                    cout << "指令出错！" << endl;
                    break;
                }
            }
            return res;
        }
        //查询id
        pair<std::unordered_map<size_t, Identity>::iterator, bool> FindIdentity(size_t name)
        {
            auto res = _identitys.find(name);

            if (res != _identitys.end())
            {
                return { res,true };
            }
            return { res,false };
        }
        //随机数概率产生
        bool ExecutionProbability(double x)
        {
            double y = x * 100;
            std::random_device rd; // 用于生成随机种子
            std::mt19937 gen(rd()); // 使用随机种子初始化随机数生成器
            std::uniform_int_distribution<int> dis(0, 99); // 定义一个均匀分布的整数随机数分布器，范围为0到99
            int number = dis(gen);
            if (number < y)
            {
                return true;
            }
            return false;
        }
    };
}
